<%@page contentType="text/html" import="java.util.*,java.io.*,java.net.*,java.text.*"%>
<%@page import="javax.naming.*,java.sql.*,javax.sql.*"%>
<html>
<head><title>${pageContext.request.contextPath}</title></head>
<body>
<%
String dsName = request.getParameter("dsName");
if (dsName == null) {
	dsName = "java:TestdbDS";
}
request.setAttribute("dsName", dsName);
String query = request.getParameter("query");
if (query == null) {
	query = "select now()";
}
request.setAttribute("query", query);
%>
<form method="post">
<table>
	<tr>
		<td>DataSource Name</td>
		<td><input type="text" name="dsName" value="${dsName}"/></td>
	</tr>
	<tr>
		<td>Test Query</td>
		<td><textarea cols="25" rows="5" name="query">${query}</textarea></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit"/></td>
	</tr>
</table>
</form>
<pre>
<%
if ("POST".equals(request.getMethod())) {
	out.println("Testing DataSource: " + dsName + " with query: " + query);
	Context ctx = null;
	Connection conn = null;
	try {
		ctx = new InitialContext();
		DataSource ds = (DataSource)ctx.lookup(dsName);
		conn = ds.getConnection();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(query);
		int i = 1;
		while(rs.next()) {
			out.println("Result#" + i + ": " + rs.getObject(i++));
		}
	} finally {
		if (ctx != null) {
			try { ctx.close(); } catch (NamingException e) { throw new RuntimeException(e); }
		}
		if (conn != null) {
			try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); }
		}
	}
}
%>
</pre>
</body>
</html>
